package yc.tool.dbmigrate.conf;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import yc.tool.dbmigrate.entity.SyncStatus;

/**
 * 同步状态管理
 * @author yuancan
 *
 */
public class SyncStatusManager {

	private static final Logger lOG = LoggerFactory.getLogger(SyncStatusManager.class);
	private static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final String SYNCSTATUS_FILEPATH = "/config/sync-status.properties"; 
	
	//同步状态
	private static final String LAST_SYNC_STATUS = "LastSyncStatus";
	private static final String LAST_SYNC_DATE = "LastSyncDate";
	private static final String LAST_SYNC_TIME = "LastSyncTime";
	private static final String TOTAL_SYNC_TIME = "TotalSyncTime";
	private static final String TOTAL_TRY_NUMBER = "TotalTryNumber";
	
	//同步状态前缀
	private static final String SYNC_STATUS_PREFIX = "P";
	
	/**
	 * 获取同步状态
	 * @return
	 * @throws IOException 
	 */
	public static SyncStatus getSyncStatus() throws IOException {
		Properties pro = new Properties();

		try(FileInputStream in = new FileInputStream(SYNCSTATUS_FILEPATH)) {
			pro.load(in);
		}
		return new SyncStatus(getIntFromPro(pro, LAST_SYNC_STATUS), getDateFromPro(pro, LAST_SYNC_DATE), 
				getIntFromPro(pro, LAST_SYNC_TIME), getIntFromPro(pro, TOTAL_SYNC_TIME), getIntFromPro(pro, TOTAL_TRY_NUMBER), "", getStatusMap(pro));
	}
	
	/**
	 * 持久化同步状态
	 * @param comment
	 * @param lastSyncStatus
	 * @param lastSyncDate
	 * @param lastSyncTime
	 * @param totalSyncTime
	 * @param totalTryNumber
	 * @throws IOException
	 */
	public static void setSyncStatus(SyncStatus syncStatus) throws IOException {
		Properties pro = new Properties();
		FileOutputStream oFile = null;
		try {
			oFile = new FileOutputStream(SYNCSTATUS_FILEPATH);
			pro.store(oFile, syncStatus.getComment());
			pro.put(LAST_SYNC_STATUS, syncStatus.getLastSyncStatus());
			pro.put(LAST_SYNC_DATE, SDF.format(syncStatus.getLastSyncDate()));
			pro.put(LAST_SYNC_TIME, syncStatus.getLastSyncTime());
			pro.put(TOTAL_SYNC_TIME, syncStatus.getTotalSyncTime());
			pro.put(TOTAL_TRY_NUMBER, syncStatus.getTotalTryNumber());
		} finally {
			oFile.close();
		}
	}
	
	private static Integer getIntFromPro(Properties pro, String proKey) {
		String str = pro.getProperty(proKey);
		Integer va = null;
		try {
			va = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			lOG.info("配置" + proKey + "=" + str + "有误，请检查程序。现在取默认值");
		}
		
		return va;
	}
	
	private static Map<Integer, Integer> getStatusMap(Properties pro) {
		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		
		String itemKeyStr = null;
		Integer itemValue = null;
		for (Object key : pro.keySet()) {
			itemKeyStr = key.toString().trim();
			if (null != key && itemKeyStr.length() > 1 && SYNC_STATUS_PREFIX.equals(itemKeyStr.substring(0, 1))) {
				try {
					int mapKey = Integer.parseInt(itemKeyStr.substring(1));
					itemValue = Integer.parseInt(pro.getProperty(key.toString()));
					result.put(mapKey, itemValue);
				} catch (Exception e) {}
				
			}
		}
		
		return result;
	}
	
	private static Date getDateFromPro(Properties pro, String proKey) {
		String str = pro.getProperty(proKey);
		Date va = null;
		
		try {
			va = SDF.parse(str);
		} catch (ParseException e) {
			lOG.info("配置" + proKey + "=" + str + "有误，请检查程序。现在取默认值" + va.toString());
		}
		
		return va;
	}
	
//	public static void 
}
